package com.leinao.open;

import javax.servlet.http.HttpServletRequest;

import com.leinao.category.dto.CategoryCount;
import com.leinao.category.service.CategoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.alibaba.fastjson.JSON;
import com.leinao.commons.Response;
import com.leinao.constant.Constant;
import com.leinao.constant.ProjectErrorEnum;
import com.leinao.constant.PublicProjectSortTypeEnum;
import com.leinao.login.dto.LoginResultDto;
import com.leinao.project.service.ProjectService;
import com.leinao.util.page.PageInfo;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * @author  wangshoufa 
 * @date 2019年1月22日 下午4:00:47
 *
 */

@RestController
@CrossOrigin
public class OpenProjectInfoController {
	
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private ProjectService projectService;

	@Autowired
	private CategoryService categoryService;

	@ApiOperation(value="获取公开项目信息", notes="返回公开项目信息")
	@GetMapping(value = "/openProjectInfo")
	public Response<PageInfo> openProjectInfo(
			@ApiParam(value = "排序方式", required = true) @RequestParam Integer sortType,
			@ApiParam(value = "当前页") @RequestParam(required = false) Integer cp,
			@ApiParam(value = "每页条数") @RequestParam(required = false) Integer ps,
			HttpServletRequest request) {
		logger.info("getPublicProjectList param  pageNumber: {}, pageSize: {}, sortType: {}", cp, ps,
				sortType);

		cp = null == cp ? Constant.DEFAULT_PAGE_NUMBER: cp;
		ps = null == ps ? Constant.DEFAULT_PAGE_SIZE : ps;
		// 校验排序类型
		if (!PublicProjectSortTypeEnum.containsType(sortType)) {
			return Response.error(Constant.RESPONSE_STATUS_SUCCESS, ProjectErrorEnum.INVALID_SORT_TYPE.getSeq(),
					ProjectErrorEnum.INVALID_SORT_TYPE.getDesc(), null);
		}

		return projectService.getOpenProjectInfo(sortType, cp, ps);
	}


	@ApiOperation(value = "获取公开项目和公开数据集关联的标签个数", notes = "获取公开项目和公开数据集关联的标签个数")
	@PostMapping(value = "/category/getCategoryCount")
	public Response<CategoryCount> getCategoryCount(HttpServletRequest request) {

		return categoryService.getCategoryCount();
	}
}
